package com.example.goodController;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.example.bean.Good;
import com.example.bean.GoodHistory;
import com.example.config.AuthAccess;
import com.example.service.GoodService;
import com.example.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @title: GoodCheckController
 * @Author wjf
 * @Date: 2022/11/22 18:42
 * @Version 1.0
 */
@RestController
@RequestMapping("/good")
@CrossOrigin
public class GoodCheckController {
    @Autowired
    private GoodService goodService;

    @AuthAccess
    @PostMapping("/save")
    public Result test(@RequestBody GoodHistory goodHistory) {
        LambdaQueryWrapper<Good> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Good::getGoodId,goodHistory.getGoodId())
                .eq(Good::getDepartId,goodHistory.getDepartId());
        Good one = goodService.getOne(queryWrapper);
        if (one!=null){
            return Result.ok().message("ok");
        }else {
            Good good = new Good();
            BeanUtil.copyProperties(goodHistory,good,true);
            good.setIsChange(1);
            goodService.updateById(good);
            
            return Result.fail().message("该设备不属于该部门");
        }


    }
}
